/**   
 * Copyright © 2017 Fan_繁. All rights reserved.
 * 
 * @Title: EmpController.java 
 * @Prject: anger
 * @Package: com.hzt.anger.controller 
 * @Description: TODO
 * @author: ChuanHui   
 * @date: 2017年8月4日 下午10:27:05 
 * @version: V1.0   
 */
package com.hzt.anger.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzt.anger.entity.Emp;
import com.hzt.anger.service.EmpService;

/**
 * @ClassName: EmpController
 * @Description: TODO
 * @author: ChuanHui
 * @date: 2017年8月4日 下午10:27:05
 */
@Controller
@RequestMapping("/Emp")
public class EmpController extends BaseHanlderController {

	private final Logger logger = LoggerFactory.getLogger(EmpController.class);
	@Autowired
	private EmpService empService;

	@RequestMapping(value = "allEmp")
	public String allEmp(HttpServletRequest request, Model model) {
		// 设置页码
		String currentPage = request.getParameter("currentPage");
		if (!StringUtils.isEmpty(currentPage)) {
			this.setCurrentPage(currentPage);
		} else {
			this.setCurrentPage("1");
		}
		// 封装查询参数
		Map<String, Object> map = new HashMap<>();
		String deptno = request.getParameter("deptno");
		if (!StringUtils.isEmpty(deptno)) {
			map.put("deptno", deptno);
			model.addAttribute("deptno", deptno);
		}
		// 调用插件进行分页
		PageHelper.startPage(this.getCurrentPage(), 5);
		List<Emp> list = empService.allEmp(map);
		PageInfo<Emp> pageinfo = new PageInfo<>(list);
		model.addAttribute("pageinfo", pageinfo);
		return "emp/emplist";
	}

	@RequestMapping(value = "/toEmp", method = RequestMethod.GET)
	public String toEmp(HttpServletRequest request, Model model) {
		// 页面跳转
		String empno = request.getParameter("empno");
		if (!StringUtils.isEmpty(empno)) {
			Emp emp = empService.getEmp(Short.valueOf(empno));
			model.addAttribute("emp", emp);
			return "emp/editorsave";
		} else {
			return "emp/editorsave";
		}
	}

	@RequestMapping(value = "/editOrSaveEmp", method = RequestMethod.POST)
	public void editOrSaveEmp(Emp emp, HttpServletResponse response) throws IOException {

		boolean result = false;
		// 根据主键ID判断是新增还是修改
		Emp findEmp = empService.findEmp(emp.getEmpno());
		if (findEmp != null) {
			result = empService.editEmp(emp) > 0;
		} else {
			result = empService.saveEmp(emp) > 0;
		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		PrintWriter writer;
		writer = response.getWriter();
		writer.println("<script type=\"text/javascript\">");
		if (result) {
			writer.println("alert('success!')");
		} else {
			writer.println("alert('failed!')");
		}
		writer.println("parent.location.reload();");
		writer.println("</script>");
		writer.flush();
		writer.close();
	}

	@RequestMapping(value = "/removeEmp")
	@ResponseBody
	public boolean removeEmp(String empno) {
		if (!StringUtils.isEmpty(empno)) {
			Short s = Short.valueOf(empno);
			return empService.removeEmp(s) > 0;
		} else {
			return false;
		}
	}
}
